package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.model.SpuImage;
import com.atguigu.gmall.product.model.SpuInfo;
import com.atguigu.gmall.product.model.SpuSaleAttr;
import com.atguigu.gmall.product.service.SpuManageService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/admin/product")
public class SpuManageController {
    @Autowired
    private SpuManageService spuManageService;

    /**
     * spu分页列表根据id查找
     * @param page
     * @param size
     * @param category3Id
     * @return
     */
    @GetMapping("/{page}/{size}")
    public Result page(@PathVariable("page") Long page, @PathVariable("size") Long size, @RequestParam("category3Id") Long category3Id ){
        Page<SpuInfo> spuInfoPage = new Page<SpuInfo>(page, size);
      Page<SpuInfo> spuInfoPage1=  spuManageService.getPage(spuInfoPage,category3Id);
      return Result.ok(spuInfoPage1);
    }

    /**
     * 保存spu
     * @param spuInfo
     * @return
     */
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        spuManageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    /**
     * 根据spuId 查询销售属性
     * @param spuId
     * @return
     */
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttrList(@PathVariable("spuId") Long spuId){
      List<SpuSaleAttr>  spuSaleAttrList=spuManageService.getSpuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }

    /**
     *根据spuId 获取spuImage 集合
     * @param spuId
     * @return
     */
    @GetMapping("/spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable("spuId") Long spuId){
         List<SpuImage> list=  spuManageService.getSpuImageList(spuId);
        return Result.ok(list);
    }

    /**
     * 根据id查找spuinfo属性
     * @param id
     * @return
     */
    @GetMapping("/getSpuInfo/{id}")
    public Result getSpuInfo(@PathVariable("id") Long id){
       SpuInfo spuInfo= spuManageService.getSpuInfo(id);
       return Result.ok(spuInfo);

    }

}
